﻿using MengXunAppTool.DAL;
using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 软件发布平台.View
{
    /// <summary>
    /// AppRecordView.xaml 的交互逻辑
    /// </summary>
    public partial class AppRecordView : UserControl
    {
        private VerDAL verDAL = new VerDAL();
        private RunDAL runDAL = new RunDAL();
        ViewModel.AppRecordViewModel viewModel = new ViewModel.AppRecordViewModel();
        List<Sql3Helper.MX_AppVerRecord> mX_Apps=new List<Sql3Helper.MX_AppVerRecord>();
        List<ViewModel.AppRecordViewModel.AppItem> appitems = new List<ViewModel.AppRecordViewModel.AppItem>();
        public SolidColorBrush FirstColor = new SolidColorBrush(Color.FromRgb(152, 214, 230));
        public SolidColorBrush SecondColor = new SolidColorBrush(Color.FromRgb(238, 238, 133));
        public AppRecordView()
        {
            InitializeComponent();
            InitRecord();
            this.DataContext = viewModel;
        }
        private void AppRecordView_Loaded(object sender, RoutedEventArgs e)
        {



        }
        public void InitRecord()
        {
            try
            {
                CbxCus.ItemsSource = verDAL.GetCustomers();
                CbxCus.SelectedIndex = -1;
                CbxPro.SelectedIndex = -1;
                CbxPro.SelectedIndex = -1;
                CbxTime.SelectedIndex = -1;
                mX_Apps = verDAL.GetApps().OrderBy(it => it.Customer).ThenBy(it => it.Product).ThenBy(it => it.AppName).ThenByDescending(it => it.ReleaseTime).ToList();
                viewModel.AppItems.Clear();
                List<ViewModel.AppRecordViewModel.AppItem> items = new List<ViewModel.AppRecordViewModel.AppItem>();
                int a = 0;
                SolidColorBrush brush = FirstColor;
                for (int i = 0; i < mX_Apps.Count; i++)
                {
                    ViewModel.AppRecordViewModel.AppItem appItem = new ViewModel.AppRecordViewModel.AppItem()
                    {
                        AppId = "X" + mX_Apps[i].id,
                        ID = mX_Apps[i].id,
                        Cus = mX_Apps[i].Customer,
                        Pro = mX_Apps[i].Product,
                        AppName = mX_Apps[i].AppName,
                        Ver = mX_Apps[i].Version,
                        ReleaseUser = mX_Apps[i].ReleaseUser,
                        ReleaseTime = mX_Apps[i].ReleaseTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        CheckUser1 = mX_Apps[i].CheckUser1,
                        CheckUser2 = mX_Apps[i].CheckUser2,
                        CheckTime = mX_Apps[i].CheckTime,
                        PushUser = mX_Apps[i].PushUser,
                        ReleaseLog = mX_Apps[i].ReleaseLog
                    };
                    if (MainWindow.userModel.nickName.ToLower() != "user" && MainWindow.userModel.nickName.ToLower() != "admin" && (MainWindow.userModel.nickName != mX_Apps[i].ReleaseUser))
                    {
                        continue;
                    }
                    if (mX_Apps[i].Enable.ToLower() == "false")
                    {
                        appItem.Enable = false;
                    }
                    if (MainWindow.userModel.nickName.ToLower() == "admin" || (MainWindow.userModel.nickName == mX_Apps[i].ReleaseUser))
                    {
                        appItem.IsAdmin = true;
                    }
                    if (i > 0 &&
                            (mX_Apps[i - 1].AppName != appItem.AppName
                            || mX_Apps[i - 1].Customer != appItem.Cus
                            || mX_Apps[i - 1].Product != appItem.Pro))
                    {
                        if (brush == FirstColor)
                        {
                            brush = SecondColor;
                        }
                        else
                        {
                            brush = FirstColor;
                        }
                    }
                    appItem.BackColor = brush;
                    items.Add(appItem);
                }
                appitems = items;
                viewModel.AppItems = items;

            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }
        private void CbxCus_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                CbxCus.ItemsSource = verDAL.GetCustomers();
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CbxCus_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "")
                {
                    List<ViewModel.AppRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text).ToList();
                    if (CbxTime.Text != "")
                    {
                        viewModel.AppItems = GetItems(items);
                    }
                    else
                    {
                        viewModel.AppItems = items;
                    }
                    CbxPro.Text = "";
                    CbxName.Text = "";
                    CbxName.ItemsSource=new List<string>();
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CbxPro_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "")
                {
                    CbxPro.ItemsSource = verDAL.GetProducts(CbxCus.Text);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }

        }

        private void CbxPro_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "")
                {
                    List<ViewModel.AppRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text).ToList();
                    if (CbxTime.Text != "")
                    {
                        viewModel.AppItems = GetItems(items);
                    }
                    else
                    {
                        viewModel.AppItems = items;
                    }
                    CbxName.Text = "";
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }

        }


        private void CbxName_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "")
                {
                    CbxName.ItemsSource = verDAL.GetAppNames(CbxCus.Text, CbxPro.Text);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CbxName_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "" && CbxName.Text != "")
                {
                    List<ViewModel.AppRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text && it.AppName == CbxName.Text).ToList();
                    if (CbxTime.Text != "")
                    {
                        viewModel.AppItems = GetItems(items);
                    }
                    else
                    {
                        viewModel.AppItems = items;
                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }
        private void CheckBoxChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox a = (e.OriginalSource as UIElement) as CheckBox;
                Sql3Helper.MX_AppVerRecord item = mX_Apps.Where(it => it.id == int.Parse(a.Content.ToString())).First();
                item.Enable = a.IsChecked.ToString();
                verDAL.ChangeMX_AppVerRecord(item);
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            string id = button.Content.ToString().Replace("X", "");
            Sql3Helper.MX_AppVerRecord app = mX_Apps.Where(it => it.id == int.Parse(id)).First();
            var dialogResult = HandyControl.Controls.MessageBox.Show("是否删除:" + "\r\n" + app.Customer + "-" + app.Product + "\r\n" + app.AppName + "-" + app.Version, button: MessageBoxButton.YesNo, icon: MessageBoxImage.Question);
            if (dialogResult == MessageBoxResult.Yes)
            {
                verDAL.DelMX_AppVerRecord(int.Parse(id));
                InitRecord();
            }
        }
        private List<ViewModel.AppRecordViewModel.AppItem> GetItems(List<ViewModel.AppRecordViewModel.AppItem> appItems)
        {
            List<ViewModel.AppRecordViewModel.AppItem> items = new List<ViewModel.AppRecordViewModel.AppItem>();
            for (int i = 0; i < appItems.Count; i++)
            {
                string cus = appItems[i].Cus;
                string pro = appItems[i].Pro;
                string name = appItems[i].AppName;
                if (i > 0 && cus == appItems[i - 1].Cus && pro == appItems[i - 1].Pro && name == appItems[i - 1].AppName)
                {
                    continue;
                }
                Sql3Helper.MX_AppRunRecord runModel = runDAL.GetAppLastRun(cus, pro, name);
                if (runModel != null )
                {
                    if (CbxTime.Text == "从未使用")
                    {
                        continue;
                    }
                    int month = 3;
                    switch (CbxTime.Text)
                    {
                        case "三月以上未使用": month = 3; break;
                        case "半年以上未使用": month = 6; break;
                        case "一年以上未使用": month = 12; break;
                        case "两年以上未使用": month = 24; break;
                        case "三年以上未使用": month = 36; break;
                        default:
                            break;
                    }
                    TimeSpan timeSpan = DateTime.Now - runModel.RunTime.AddMonths(month);
                    if (timeSpan.Seconds > 0)
                    {
                        items.Add(appItems[i]);
                    }
                }
                else if (CbxTime.Text == "从未使用")
                {
                    items.Add(appItems[i]);
                }
            }
            return items;
        }

        private void CbxTime_DropDownClosed(object sender, EventArgs e)
        {

            List<ViewModel.AppRecordViewModel.AppItem> items = new List<ViewModel.AppRecordViewModel.AppItem>();
            if (CbxCus.Text != "" && CbxPro.Text != "" && CbxName.Text != "")
            {
                items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text && it.AppName == CbxName.Text).ToList();
            }
            else if (CbxCus.Text != "" && CbxPro.Text != "" )
            {
                items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text ).ToList();
            }
            else if (CbxCus.Text != "" )
            {
                items = appitems.Where(it => it.Cus == CbxCus.Text).ToList();
            }
            else
            {
                items = appitems;
            }
            if (CbxTime.Text != "")
            {
                viewModel.AppItems = GetItems(items);
            }
            else
            {
                viewModel.AppItems = items;
            }
        }
    }
}
